<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org" xmlns:sec="http://www.pollix.at/thymeleaf/shiro">
<head>
    <th:block th:include="include :: header('预约管理表列表')"/>
</head>
<body class="pear-container">
<div class="layui-card">
    <div class="layui-card-body">
        <form class="layui-form" action="">
            <div class="layui-form-item">
                <label class="layui-form-label">名称</label>
                <div class="layui-input-inline">
                    <input type="text" name="searchText" placeholder="" class="layui-input">
                </div>
                <button class="pear-btn pear-btn-md pear-btn-primary" lay-submit lay-filter="orderlist-query">
                    <i class="layui-icon layui-icon-search"></i>
                   	 查询
                </button>
                <button type="reset" class="pear-btn pear-btn-md">
                    <i class="layui-icon layui-icon-refresh"></i>
					重置
                </button>
            </div>
        </form>
    </div>
</div>
<div class="layui-card">
    <div class="layui-card-body">
        <table id="orderlist-table" lay-filter="orderlist-table"></table>
    </div>
</div>
</body>

<script type="text/html" id="orderlist-toolbar">
    <button shiro:hasPermission="gen:orderlist:add" class="pear-btn pear-btn-primary pear-btn-md" lay-event="add">
        <i class="layui-icon layui-icon-add-1"></i>
        新增
    </button>
    <button shiro:hasPermission="gen:orderlist:remove" class="layui-btn layui-btn-danger pear-btn-md" lay-event="batchRemove">
        <i class="layui-icon layui-icon-delete"></i>
		删除
    </button>
    <button shiro:hasPermission="gen:orderlist:remove" class="layui-btn layui-btn-normal pear-btn-md" lay-event="unPay">
        <i class="layui-icon">&#xe609;</i>
        挂账
    </button>
    <button shiro:hasPermission="gen:orderlist:remove" class="layui-btn layui-btn-warm pear-btn-md" lay-event="pay">
        <i class="layui-icon">&#xe600;</i>
        结账  
    </button>

</script>

<script type="text/html" id="orderlist-bar">


    <button shiro:hasPermission="gen:orderlist:edit" class="pear-btn pear-btn-primary pear-btn-sm" lay-event="edit" title="修改"><i class="layui-icon layui-icon-edit"></i>
    </button>

    <button shiro:hasPermission="gen:orderlist:remove" class="pear-btn pear-btn-danger pear-btn-sm" lay-event="remove" title="删除"><i class="layui-icon layui-icon-delete"></i>
    </button>

</script>


<script type="text/html" id="orderlist-type">
    {{#if (d.dataState == '0') { }}
    <span class="layui-badge layui-bg-orange">开启</span>
    {{# }else if(d.dataState == '-1'){ }}
    <span class="layui-badge layui-bg-blue">关闭</span>
    {{# } }}
</script>

<th:block th:include="include :: footer"/>
<script>
    layui.use(['table', 'form', 'jquery'], function () {
        let table = layui.table;
        let form = layui.form;
        let $ = layui.jquery;

        let MODULE_PATH = rootPath+"/OrderlistController/";
        let cols = [
            [
                {type: 'checkbox'},
                        {title: '预约ID', field: 'id', align: 'center'},
                        {title: '顾客姓名', field: 'customerName', align: 'center'},
                        {title: '顾客电话', field: 'customerTel', align: 'center'},
                        {title: '预约项目', field: 'oProj', align: 'center'},
                        {title: '发型师姓名', field: 'hName', align: 'center'},
                        {title: '发型师助理', field: 'hAss', align: 'center'},
                        {title: '预计开始时间', field: 'customerTime', align: 'center'},
                        {title: '预计结束时间', field: 'eTime', align: 'center'},
                        {title: '单据状态', field: 'rState', align: 'center'},


                {title: '操作', toolbar: '#orderlist-bar', align: 'center'}
            ]
        ]

        //table表单列表
        table.render({
            elem: '#orderlist-table',
            url: MODULE_PATH + 'list',
            page: true,
            method: 'get',
            cols: cols,
            skin: 'line',
            toolbar: '#orderlist-toolbar',
            defaultToolbar: [{
                layEvent: 'refresh',
                icon: 'layui-icon-refresh',
            }, 'filter', 'print', 'exports']
        });

        //table图标事件绑定
        table.on('tool(orderlist-table)', function (obj) {
            if (obj.event === 'remove') {
                window.remove(obj);
            } else if (obj.event === 'edit') {
                window.edit(obj);
            }
        });

        //table工具栏绑定
        table.on('toolbar(orderlist-table)', function (obj) {
            if (obj.event === 'add') {
                window.add();
            } else if (obj.event === 'refresh') {
                window.refresh();
            } else if (obj.event === 'batchRemove') {
                window.batchRemove(obj);
            }else if (obj.event === 'unPay') {
                window.unPay(obj);
            }else if (obj.event === 'pay') {
                window.pay(obj);
            }
        });

        //查询按钮绑定
        form.on('submit(orderlist-query)', function (data) {
            table.reload('orderlist-table', {where: data.field})
            return false;
        });


        //添加
        window.add = function () {
            layer.open({
                type: 2,
                title: '新增',
                shade: 0.1,
                area: ['500px', '500px'],
                content: MODULE_PATH + 'add'
            });
        }
        //修改
        window.edit = function (obj) {
            layer.open({
                type: 2,
                title: '修改',
                shade: 0.1,
                area: ['500px', '500px'],
                content: MODULE_PATH + 'edit/' + obj.data['id']
            });
        }
        //删除
        window.remove = function (obj) {
            layer.confirm('确定要删除该数据', {icon: 3, title: '提示'}, function (index) {
                layer.close(index);
                let loading = layer.load();
                $.ajax({
                    url: MODULE_PATH + "remove?ids=" + obj.data['id'],
                    dataType: 'json',
                    type: 'delete',
                    success: function (result) {
                        layer.close(loading);
                        if (result.code==200) {
                            layer.msg(result.msg, {icon: 1, time: 1000}, function () {
                                obj.del();
                            });
                        } else {
                            layer.msg(result.msg, {icon: 2, time: 1000});
                        }
                    }
                })
            });
        }

        //批量删除
        window.batchRemove = function (obj) {
            let data = table.checkStatus(obj.config.id).data;
            if (data.length === 0) {
                layer.msg("未选中数据", {icon: 2, time: 1000});
                return false;
            }
            let ids = "";
            for (let i = 0; i < data.length; i++) {
                ids += data[i].id + ",";
            }
            ids = ids.substr(0, ids.length - 1);
            layer.confirm('确定要删除这些数据', {icon: 3, title: '提示'}, function (index) {
                layer.close(index);
                let loading = layer.load();
                $.ajax({
                    url: MODULE_PATH + "remove?ids=" + ids,
                    dataType: 'json',
                    type: 'delete',
                    success: function (result) {
                        layer.close(loading);
                        if (result.code==200) {
                            layer.msg(result.msg, {icon: 1, time: 1000}, function () {
                                table.reload('orderlist-table');
                            });
                        } else {
                            layer.msg(result.msg, {icon: 2, time: 1000});
                        }
                    }
                })
            });
        }


      
        // $(document).on('click',"#unPay",function(obj){

        //     layer.msg("按钮点击");

        // });



         //挂账
        window.unPay = function (obj) {
            let data = table.checkStatus(obj.config.id).data;
            console.log(obj);
            if (data.length === 0) {
                layer.msg("未选中数据", {icon: 3, time: 1000});
                return false;
            }
            if (data.length > 1) {
                layer.msg("一次只能操作一条数据", {icon: 3, time: 1000});
                return false;
            }else{
                layer.confirm('确定改为挂账状态？', {icon: 3, title: '提示'}, function (index) {
                    layer.close(index);
                    let loading = layer.load();
                    console.log(data);
                    data[0].rState = "挂账中";
                    console.log('表单对象：', data[0]);

                    $.ajax({
                        url: MODULE_PATH+'/edit',
                        data: data[0],
                        dataType: 'json',
                        contentType: 'application/x-www-form-urlencoded',
                        type: 'post',
                        success: function (result) {
                            layer.close(loading);
                            if (result.code==200) {
                                layer.msg("修改成功！", {icon: 1, time: 1000}, function () {
                                    table.reload('orderlist-table');
                                });
                            }else {
                                layer.msg(result.msg, {icon: 2, time: 1000});
                            }
                        }
                        })
                     }           
                )}   
        }


        //结账
        window.pay = function (obj) {
            let data = table.checkStatus(obj.config.id).data;
            console.log(obj);
            console.log(data.length);

            if (data.length === 0) {
                layer.msg("未选中数据", {icon: 3, time: 1000});
                return false;
            }
            if (data.length > 1) {
                layer.msg("一次只能操作一条数据", {icon: 3, time: 1000});
                return false;
            }
            if(data[0].rState != "挂账中"){
                layer.msg("只有挂账中的订单才能结账！", {icon: 2, time: 1000});
                return false;
            }          

            let ids = data[0].id;   
            console.log(ids);    
            console.log(data[0].rState);      
            layer.open({
                type: 2,
                title: '结账',
                shade: 0.1,
                area: ['600px', '500px'],
                content: MODULE_PATH + 'pay1/' + ids
            });
        }



        //刷新按钮
        window.refresh = function () {
            table.reload('orderlist-table');
        }
    })
</script>
</html>